package com.bwie.esdemo0122.controller;

import com.bwie.esdemo0122.service.EsDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Description: es文档控制器
 * @Version: 1.0
 */
@RestController
@RequestMapping("/doc")
public class EsDocController {

    @Autowired
    private EsDocService esDocService;

    /**
     * @Description: 保存文档
     */
    @PostMapping("/save")
    public boolean save(@RequestBody Map<String, Object> esData) {
        return esDocService.save(esData);
    }
    /**
     * @Description: 更新文档
     */
    @RequestMapping("/update")
    public boolean update(@RequestBody Map<String, Object> esData) {
        return esDocService.update(esData);
    }
    /**
     * @Description: 删除文档
     */
    @RequestMapping("/delete")
    public boolean delete(@RequestParam(value = "indexName") String indexName,
                         @RequestParam(value = "docId") String docId) {
        return esDocService.delete(indexName, docId);
    }
    /**
     * @Description: 查询文档
     * http://localhost:8081/doc/query?indexName=my_index
     */
    @RequestMapping("/query")
    public Map<String, Object> query(@RequestParam(value = "indexName") String indexName,
                                     @RequestParam(value = "docId", required = false) String docId) {
        long startTime = System.currentTimeMillis();
        Map<String, Object> query = esDocService.query(indexName, docId);
        long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime) + "ms");
        return query;
    }
}
